import {
  getListPage,
  changeInvoiceStatus,
  deleteRow,
  deleteBatch,
  sendOffInvoice,
  findDelivery
} from '@/api/invoice'
import {
  UploaderImage,
  Kindeditor
} from '@/layout/components'
const statusMap = ['全部', '待审核', '已驳回', '已通过', '已寄出']
export default {
  components: {
    UploaderImage,
    Kindeditor
  },
  filters: { // 字段转换
    statusFilter(status) {
      // const statusMap = {
      //   published: 'success',
      //   draft: 'gray',
      //   deleted: 'danger'
      // }
      return statusMap[status]
    }
  },
  data() {
    return {
      statusMap: statusMap,
      multipleSelection: [], // 选中的item
      empty_text: '暂无数据',
      listPages: {
        page: 1,
        size: 10,
        sizes: [5, 10, 15, 20, 30, 50, 100],
        total: 0
      },
      listParam: {
        status: null,
        pageNum: 1,
        pageSize: 10
      },
      datas: null,
      listLoading: false,
      checkFormDialog: {
        visible: false,
        action: '',
        title: '提示',
        width: '30%',
        center: true,
        data: {
          status: 0,
          reason: '',
          id: ''
        }
      },
      sendformDialog: {
        visible: false,
        action: '',
        title: '提示',
        width: '30%',
        center: true,
        data: {
          id: '',
          waybillNumber: '',
          invoiceNo: '',
          numbers: ''
        },
        disabled: false
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this._fetchData()
  },
  watch: {
    listParam: {
      handler() {
        this._fetchData()
      },
      deep: true // 深度监听，同时也可监听到param参数变化
    }
  },
  methods: {
    _fetchData() {
      // this.listLoading = true
      getListPage(this.listParam).then(response => {
        this.datas = response.result.list
        this.listPages.total = response.result.total
        this.listLoading = false
      })
    },
    statusFilter(status) {
      return this.statusMap[status]
    },
    _page_size_change(size) {
      // console.log(`每页 ${size} 条`)
      this.listParam.pageSize = this.listPages.size = size
    },
    _page_prev_click() {
      this.listPages.page--
      this.listParam.pageNum = this.listPages.page
    },
    _page_current_change(page) {
      // console.log(`当前页: ${page}`)
      this.listParam.pageNum = this.listPages.page = page
    },
    _page_next_click() {
      this.listPages.page++
      this.listParam.pageNum = this.listPages.page
    },
    _fromUploaderImage(data) {
      console.log(data)
      this.formData.coverPath = data
    },
    _fromKindeditor(data) {
      console.log(data)
      this.formData.content = data
    },
    checkRow(row, type) {
      this.checkFormDialog.visible = true
      this.checkFormDialog.title = '发票审核'
      this.checkFormDialog.action = 'edit'
      console.log(this.formData)
      console.log(row)
      this.checkFormDialog.data = row
    },
    // 全选change
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 批量通过
    checkBatch() {
      let ids = ''
      this.multipleSelection.forEach(item => {
        ids += ids === '' ? item.id : ',' + item.id
      })
      this._checkFormSubmit(ids, 3, '通过')
    },
    // 确认驳回
    turnDown() {
      this.checkFormDialog.visible = false
      this._checkFormSubmit(this.checkFormDialog.data.id, 2, this.checkFormDialog.data.reason)
    },
    // 通过
    pass(row, status) {
      this.$confirm('确定通过该发票的审核?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this._checkFormSubmit(row.id, status, '通过')
      })
    },
    // ids: 订单ids status:订单状态：1：待审核 2：驳回 3：通过 4：已寄出
    _checkFormSubmit(ids, status, turnDown) {
      const param = {
        id: ids,
        status: status,
        turnDown: turnDown
      }
      changeInvoiceStatus(param).then(response => {
        if (response.code === 200 || response.code === 20000) {
          this._fetchData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        } else {
          this.$message({
            type: 'info',
            message: '操作失败'
          })
        }
      })
    },
    sendFormRow(row) {
      this.sendformDialog.visible = true
      this.sendformDialog.title = '寄出发票'
      this.sendformDialog.action = 'add'
      this.sendformDialog.data.id = row.id
    },
    lookformRow(row) {
      this.sendformDialog.visible = true
      this.sendformDialog.title = '查看发票'
      this.sendformDialog.action = 'edit'
      // console.log(this.sendformDialog.data)
      // console.log(row)
      findDelivery({ id: row.id }).then(response => {
        this.sendformDialog.disabled = true
        this.sendformDialog.data = response.result
      })
    },
    _sendformSubmit() {
      this.sendformDialog.visible = false
      const param = {
        id: this.sendformDialog.data.id,
        invoiceNo: this.sendformDialog.data.invoiceNo,
        waybillNumber: this.sendformDialog.data.waybillNumber
      }
      sendOffInvoice(param).then(response => {
        if (response.code === 200 || response.code === 20000) {
          this._fetchData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        } else {
          this.$message({
            type: 'info',
            message: '操作失败'
          })
        }
      })
    },
    detailRow(row) {
      this.$router.push({
        path: 'detail',
        query: {
          id: row.id
        }
      })
    },
    deleteRow(row, index) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((result) => {
        deleteRow({
          id: row.id
        }).then(response => {
          if (response.code === 200 || response.code === 20000) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.datas.splice(index, 1)
          } else {
            this.$message({
              type: 'info',
              message: '操作失败'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '删除取消'
        })
      })
    },
    deleteMore() {
      deleteBatch().then(response => {

      })
    }
  }
}
